﻿CREATE Function dbo.f_CurrCreditCount
(@LoanID int,@AccType char(1) = '',@Pulled bit )
RETURNS int WITH SCHEMABINDING BEGIN

IF @Pulled <> 1 RETURN dbo.RegistryValue('DefaultRuleValue')

DECLARE @Ret int

IF ISNULL(@AccType,'') = ''
	SELECT @Ret = count(*)
	FROM dbo.v_TradeReferences
	WHERE LoanID = @LoanID AND (AccountState = 1)
ELSE
	SELECT @Ret = count(*)
	FROM dbo.v_TradeReferences
	WHERE LoanID = @LoanID AND AccType=@AccType AND (AccountState = 1)

RETURN  ISNULL(@Ret,0)
END


